<?php
namespace Infinity;

/* 已经过时不再使用 */

class Entity {

  private $entity_type;
  private $entity_id;
  private $fields;
  private $entity_info;

  /**
   * @param string $entity_type
   * @param object|int|array $entity
   */
  public function __construct($entity_type, $entity=array()) {
    $entity_id = self::getKeyName($entity_type);
    $this->entity_id = $entity_id;
    $this->entity_type = $entity_type;
    if(is_array($entity)) {
      $entity = entity_create($this->entity_type, $entity);
    }
    $this->entity_info = entity_get_info($entity_type);
    $this->setObject($entity);
  }

  /**
   * 获取entity的对应ID名
   * @param $entity_type
   * @return string
   */
  public static function getKeyName($entity_type)
  {
    $info = entity_get_info($entity_type);
    return isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'];
  }

  /**
   * 从一个对象复制出entity
   * @param $entity_type
   * @param $entity
   * @return Entity
   */
  public static function cloneFrom($entity_type, $entity) {
    $entity->nid = NULL;
    $entity->vid = NULL;
    $entity->tnid = NULL;
    $entity->uid = $GLOBALS['user']->uid;
    $entity->created = time();
    $entity->updated = NULL;
    return new Entity($entity_type, $entity);
  }

  /**
   * @param string $entity_type
   * @param object|int $entity
   * @param string $entity_id
   * @return Entity
   */
  public static function load($entity_type, $entity, $entity_id='nid')
  {
    $static = &drupal_static(__CLASS__.'::'.__FUNCTION__, array());
    $token = is_object($entity) ? md5(serialize($entity)) : $entity;
    if(!isset($static[$entity_type][$token])) {
      $static[$entity_type][$token] = new Entity($entity_type, $entity, $entity_id);
    }
    return $static[$entity_type][$token];
  }

  function __get($property_name)
  {
    return $this->fieldManager()->$property_name;
  }

  function __set($property_name, $value)
  {
    $this->fieldManager()->$property_name = $value;
  }

  public function get($property_name)
  {
    return $this->fieldManager()->$property_name;
  }

  public function set($property_name, $value)
  {
    $this->fieldManager()->$property_name = $value;
    return $this;
  }
  
  public function isNew()
  {
    if($this->id())
      return true;
    return false;
  }
  
  /**
   * get entity ID
   * @return int
   */
  public function id()
  {
    return entity_id($this->entity_type, $this->getObject());
  }

  public function getObject()
  {
    return $this->fieldManager()->getObject();
  }

  /**
   * view render
   * @param string $view_mode
   * @param string $lang
   * @return array
   */
  public function view($view_mode='full', $lang=LANGUAGE_NONE)
  {
    $content = drupal_static(__CLASS__.'::'.__FUNCTION__, array());
    if(!isset($content[$this->entity_type.'_'.$this->id()])) {
      if(isset($this->fieldManager()->getObject()->language)) {
        $lang = $this->fieldManager()->getObject()->language;
      }
      field_attach_prepare_view($this->entity_type, array($this->id() => $this->fieldManager()->getObject()), $view_mode, $lang);
      entity_prepare_view($this->entity_type, array($this->id() => $this->fieldManager()->getObject()), $lang);
      $content[$this->entity_type.'_'.$this->id()] = field_attach_view($this->entity_type, $this->fieldManager()->getObject(), $view_mode);
    }
    return $content[$this->entity_type.'_'.$this->id()];
  }

  /**
   * edit form render
   * @param array $form
   * @param array $form_state
   */
  public function edit(&$form, &$form_state)
  {
    field_attach_form($this->entity_type, $this->fieldManager()->getObject(), $form, $form_state);
  }

  /**
   * @return Entity
   */
  public function save()
  {
    $this->FieldManager()->save();
    $this->reload();
    return $this;
  }

  private function reload()
  {
    $this->fields = FieldManager::load($this->entity_type, $this->id(), TRUE);
  }

  private function setObject($entity)
  {
    $this->fields = FieldManager::load($this->entity_type, $entity, true);
  }

  public function isEmpty()
  {
    if($this->fields)
      return false;
    return true;
  }

  /**
   * @return FieldManager
   */
  public function fieldManager()
  {
    return $this->fields;
  }

  public function setBundle($value) {
    if(!empty($this->entity_info['entity keys']['bundle'])) {
      $this->{$this->entity_info['entity keys']['bundle']} = $value;
    }
    return $this;
  }
  
  public function bundle() {
    if(!empty($this->entity_info['entity keys']['bundle'])) {
      return $this->{$this->entity_info['entity keys']['bundle']};
    }
    return '';
  }
  
  public function label() {
    if(!empty($this->entity_info['label'])) {
      return $this->entity_info['label'];
    }
    return '';
  }
  
  public function uri() {
    if($this->isNew) return '';
    if(!empty($this->entity_info['uri callback'])) {
      $uri_callback = $this->entity_info['uri callback'];
      $arr = $uri_callback($this->getObject());
      return $arr['path'];
    }
    return '';
  }
}